/*
 * TextFieldDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "TextFieldDelegate.h"

#include "QApplicationDelegate.h"
#include "SwingDelegate.h"

// TextFieldDelegate :: Protected Constructor. ---------------------------------

/*!
 * \brief Constructs new TextFieldDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the TextField java object
 */
TextFieldDelegate::TextFieldDelegate(JNIEnv *env, jobject obj)
  : ComponentDelegate(env, obj, new QLineEdit())
{
  connectDirect(qLineEdit(), SIGNAL(returnPressed()),
                SLOT(returnPressedSlot()));
}

// TextFieldDelegate :: Native Calls. ------------------------------------------

/*!
 * \brief Returns text.
 *
 * \param env the JNIEnv pointer
 * 
 * \return text
 */
jstring TextFieldDelegate::getText(JNIEnv *env)
{
  Q_ASSERT(env);
  SWING_REENTRANT_RETURN(getText, jstring, SWING_ENV_ARG)
  return QStringAdapter::toJString(env, qLineEdit()->text());
}

/*!
 * \brief Sets text.
 *
 * \param env the JNIEnv pointer
 * \param text the text to be set
 */
void TextFieldDelegate::setText(JNIEnv *env, jstring text)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setText, SWING_ENV_ARG, JSTRING_ARG(text))
  qLineEdit()->setText(QStringAdapter(env, text));
}

/*!
 * \brief Sets horizontal alignment.
 *
 * \param env the JNIEnv pointer
 * \param alignment the horizontal alignment
 */
void TextFieldDelegate::setHorizontalAlignment(JNIEnv *env, jint alignment)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setHorizontalAlignment, SWING_ENV_ARG, JINT_ARG(alignment))
  qLineEdit()->setAlignment(SwingDelegate::toHorizontalAlignment(alignment));
}

// TextFieldDelegate :: Static Native Calls. -----------------------------------

/*!
 * \brief Initializes TextFieldDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the TextField java class
 *
 * \exception FatalError if failed
 */
void TextFieldDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT (!sm_returnPressedMID);
  if (!INIT_UMID(returnPressed, cls, "(I)V")) {
    FATAL_LOGGED_ERROR("TextFieldDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong TextFieldDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new TextFieldDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// TextFieldDelegate :: Native Callbacks. --------------------------------------

/*!
 * \brief Calls returnPressed method of the TextField java object.
 */
void TextFieldDelegate::returnPressed(jint modifiers)
{
  jniObject()->callVoidMethod(sm_returnPressedMID, modifiers);
}

// TextFieldDelegate :: Protected Member Functions. ----------------------------

/*!
 * \brief \c returnPressed slot.
 */
void TextFieldDelegate::returnPressedSlot()
{
  returnPressed(QApplicationDelegate::keyboardModifiers());
}

// TextFieldDelegate :: Private Static Data Members. ---------------------------

jmethodID TextFieldDelegate::sm_returnPressedMID = 0;
